/**
 * 
 */
package com.carsa.blackberry.domain.dto;

import com.carsa.blackberry.domain.services.webServices.impl.ZSdareAre.ZSdareAreable;
import com.carsa.blackberry.domain.services.webServices.impl.ZSdareAre.ZsdareAre;
import com.carsa.blackberry.domain.services.webServices.impl.ZSdareEntrega.ZSdareEntregable;
import com.carsa.blackberry.domain.services.webServices.impl.ZSdareEntrega.ZsdareEntrega;
import com.carsa.blackberry.util.XMLElement;
import com.carsa.blackberry.util.XMLHandler;
import com.carsa.blackberry.util.XMLPortable;

/**
 * @author Nahuel Barrios.
 * @since 12/08/2010.
 * @version 1.0
 */
public class ArticuloDTO implements ZSdareEntregable, ZSdareAreable, XMLPortable {

    private ZsdareEntrega _inputArticle  = new ZsdareEntrega();
    private ZsdareAre     _outputArticle = new ZsdareAre();

    public ArticuloDTO() {
	super();
    }

    public ArticuloDTO(String xmlSource) {
	this.initializeFromXML(xmlSource);
    }

    public ArticuloDTO(ZsdareEntrega article) {
	super();
	_inputArticle = article;
    }

    public String toString() {
	return "ArticuloDTO [  _inputArticle=" + _inputArticle + ",  _outputArticle=" + _outputArticle + "  ]";
    }

    public void setOutputFromInput() {
	// _outputArticle.setAyuda(ayuda);

	// Numero de grupo de entregas, y numero de entrega
	_outputArticle.setSammg(_inputArticle.getSammg());
	_outputArticle.setVbeln(_inputArticle.getVbeln());

	// Fecha y hora de la confirmacion por el usuario.
	// _outputArticle.setFeare(feare);
	// _outputArticle.setHoare(hoare);

	// Motivo y submotivo de entrega fallida.
	// _outputArticle.setGrund(grund);
	// _outputArticle.setSubmo(submo);

	_outputArticle.setPosnr(_inputArticle.getPosnr());
	// _outputArticle.setLfdif(lfdif);
	_outputArticle.setLfimg(_inputArticle.getLfimg());
    }

    public ZsdareEntrega getInputWSObject() {
	return this._inputArticle;
    }

    public void setOutputWSObject(ZsdareAre outputObject) {
	this._outputArticle = outputObject;
    }

    public ZsdareAre getOutputWSObject() {
	return this._outputArticle;
    }

    /**
     * @return Un {@link String} con el grupo de entregas al que pertenece el articulo.
     */
    public String getSammg() {
	return this._inputArticle.getSammg();
    }
    
    public String getGrund(){
	return this._outputArticle.getGrund();
    }
    public String getSubmo(){
	return this._outputArticle.getSubmo();
    }
    
    /**
     * @return Un {@link String} con el numero de entrega a la que pertenece el articulo.
     */
    public String getVbeln() {
	return this._inputArticle.getVbeln();
    }

    /**
     * @return La descripcion del articulo.
     */
    public String getArktx() {
	return this._inputArticle.getArktx();
    }

    /**
     * @return Un {@link String} con el codigo/numero de articulo/material.
     */
    public String getMatnr() {
	return this._inputArticle.getMatnr();
    }

    /**
     * @param ayuda
     *            the ayuda to set
     */
    public void setAyuda(String ayuda) {
	this._outputArticle.setAyuda(ayuda);
    }

    /**
     * @param feare
     *            the feare to set
     */
    public void setFeare(String feare) {
	this._outputArticle.setFeare(feare);
    }

    /**
     * @param hoare
     *            the hoare to set
     */
    public void setHoare(String hoare) {
	this._outputArticle.setHoare(hoare);
    }

    /**
     * @param grund
     *            El motivo de que sea una entrega fallida.
     */
    public void setGrund(String grund) {
	this._outputArticle.setGrund(grund);
    }

    /**
     * @param submo
     *            El submotivo de que sea una entrega fallida.
     */
    public void setSubmo(String submo) {
	this._outputArticle.setSubmo(submo);
    }

    /**
     * @return lfimg
     * 		La cantidad de unidades que hay que entregar de este articulo.
     */
    public double getLfimg(){
	return this._inputArticle.getLfimg();
    }

    /**
     * @param lfimg
     * La cantidad de unidades que hay que entregar de este articulo.
     */
    public void setLfimg(double lfimg){
	this._inputArticle.setLfimg(lfimg);
    }
    
    /**
     * @param lfdif
     */
    public void setLfdif(double lfdif) {
	this._outputArticle.setLfdif(lfdif);
    }

    public void initializeFromXML(String xmlSource) {
	XMLHandler xml = new XMLHandler(xmlSource);

	XMLHandler xmlInput = new XMLHandler(xml.getElementValue(_qname_ZSdareEntrega.getLocalPart()));
	_inputArticle.setArktx(xmlInput.getElementValue(_qname_Arktx.getLocalPart()));
	_inputArticle.setCity1(xmlInput.getElementValue(_qname_City1.getLocalPart()));
	_inputArticle.setDirec(xmlInput.getElementValue(_qname_Direc.getLocalPart()));
	_inputArticle.setLfimg(Double.parseDouble(xmlInput.getElementValue(_qname_Lfimg.getLocalPart())));
	_inputArticle.setMatnr(xmlInput.getElementValue(_qname_Matnr.getLocalPart()));
	_inputArticle.setName1(xmlInput.getElementValue(_qname_Name1.getLocalPart()));
	_inputArticle.setName2(xmlInput.getElementValue(_qname_Name2.getLocalPart()));
	_inputArticle.setPosnr(xmlInput.getElementValue(_qname_Posnr.getLocalPart()));
	_inputArticle.setSammg(xmlInput.getElementValue(_qname_Sammg.getLocalPart()));
	_inputArticle.setTelNumber(xmlInput.getElementValue(_qname_TelNumber.getLocalPart()));
	_inputArticle.setVbeln(xmlInput.getElementValue(_qname_Vbeln.getLocalPart()));
//	_inputArticle.setVbeln(xmlInput.getElementValue(_qname_Obs.getLocalPart()));// TODO : Functionality : OBS

	XMLHandler xmlOutput = new XMLHandler(xml.getElementValue(_qname_ZSdareAre.getLocalPart()));
	_outputArticle.setAyuda(xmlOutput.getElementValue(_qname_Ayuda.getLocalPart()));
	_outputArticle.setFeare(xmlOutput.getElementValue(_qname_Feare.getLocalPart()));
	_outputArticle.setGrund(xmlOutput.getElementValue(_qname_Grund.getLocalPart()));
	_outputArticle.setHoare(xmlOutput.getElementValue(_qname_Hoare.getLocalPart()));
	_outputArticle.setLfdif(Double.parseDouble(xmlOutput.getElementValue(_qname_Lfdif.getLocalPart())));
	_outputArticle.setLfimg(Double.parseDouble(xmlOutput.getElementValue(_qname_Lfimg.getLocalPart())));
	_outputArticle.setPosnr(xmlOutput.getElementValue(_qname_Posnr.getLocalPart()));
	_outputArticle.setSammg(xmlOutput.getElementValue(_qname_Sammg.getLocalPart()));
	_outputArticle.setSubmo(xmlOutput.getElementValue(_qname_Submo.getLocalPart()));
	_outputArticle.setVbeln(xmlOutput.getElementValue(_qname_Vbeln.getLocalPart()));
    }

    public String toXMLString() {
	XMLHandler xml = new XMLHandler("anArticle", null, null);

	// Exporto el objeto de entrada.
	XMLHandler xmlInput = new XMLHandler(_qname_ZSdareEntrega.getLocalPart(), null, null);

	xmlInput.addElementAtRoot(new XMLElement(_qname_Arktx.getLocalPart(), _inputArticle.getArktx()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_City1.getLocalPart(), _inputArticle.getCity1()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_Direc.getLocalPart(), _inputArticle.getDirec()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_Matnr.getLocalPart(), _inputArticle.getMatnr()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_Name1.getLocalPart(), _inputArticle.getName1()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_Name2.getLocalPart(), _inputArticle.getName2()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_Posnr.getLocalPart(), _inputArticle.getPosnr()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_Sammg.getLocalPart(), _inputArticle.getSammg()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_TelNumber.getLocalPart(), _inputArticle.getTelNumber()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_Vbeln.getLocalPart(), _inputArticle.getVbeln()));
	xmlInput.addElementAtRoot(new XMLElement(_qname_Lfimg.getLocalPart(), String.valueOf(_inputArticle.getLfimg())));
//	xmlInput.addElementAtRoot(new XMLElement(_qname_Obs.getLocalPart(), String.valueOf(_inputArticle.getObs())));// TODO : Functionality : OBS

	xml.addElementAtRoot(xmlInput.toXMLString());

	// Exporto el objeto de salida.
	XMLHandler xmlOutput = new XMLHandler(_qname_ZSdareAre.getLocalPart(), null, null);

	xmlOutput.addElementAtRoot(new XMLElement(_qname_Ayuda.getLocalPart(), _outputArticle.getAyuda()));
	xmlOutput.addElementAtRoot(new XMLElement(_qname_Feare.getLocalPart(), _outputArticle.getFeare()));
	
	String value = _outputArticle.getGrund();
	xmlOutput.addElementAtRoot(new XMLElement(_qname_Grund.getLocalPart(), value == null ? "" : value));
	
	xmlOutput.addElementAtRoot(new XMLElement(_qname_Hoare.getLocalPart(), _outputArticle.getHoare()));
	xmlOutput.addElementAtRoot(new XMLElement(_qname_Lfdif.getLocalPart(), String.valueOf(_outputArticle.getLfdif())));
	xmlOutput.addElementAtRoot(new XMLElement(_qname_Lfimg.getLocalPart(), String.valueOf(_outputArticle.getLfimg())));
	xmlOutput.addElementAtRoot(new XMLElement(_qname_Posnr.getLocalPart(), _outputArticle.getPosnr()));
	xmlOutput.addElementAtRoot(new XMLElement(_qname_Sammg.getLocalPart(), _outputArticle.getSammg()));
	
	value = _outputArticle.getSubmo();
	xmlOutput.addElementAtRoot(new XMLElement(_qname_Submo.getLocalPart(), value == null ? "" : value));
	
	xmlOutput.addElementAtRoot(new XMLElement(_qname_Vbeln.getLocalPart(), _outputArticle.getVbeln()));

	xml.addElementAtRoot(xmlOutput.toXMLString());

	return xml.toString();
    }

    /**
     * @return
     */
    // TODO : Javadoc : Terminate this Javadoc.
    public boolean wasCalifiedAsFail() {
	return this._outputArticle.getLfdif() == 1;
    }

    /**
     * @return
     */
    // TODO : Javadoc : Terminate this Javadoc.
    public boolean wasCalifiedAsSuccess() {
	return !this.wasCalifiedAsFail();
    }

}
